package com.jiyi.activity.datainput.jm;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.jiyi.bean.datainput.DataInputDQRQAQBean;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.inteface.WebApi;
import com.jiyi.widget.picselect.PicsAdapter;
import com.jiyi.widget.picshow.ImagePagerActivity;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DataInputDQRQAQReadActiviy extends ActivityBase {
	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.tvYDFHBFHYQ)
	TextView tvYDFHBFHYQ;
	@BindView(R.id.tvDQXLSFLJ)
	TextView tvDQXLSFLJ;
	@BindView(R.id.tvDQSBRLZT)
	TextView tvDQSBRLZT;
	@BindView(R.id.tvBWJSBFH)
	TextView tvBWJSBFH;
	@BindView(R.id.tvAZSFFHBZ)
	TextView tvAZSFFHBZ;
	@BindView(R.id.tvXLBJZZ)
	TextView tvXLBJZZ;
	@BindView(R.id.tvJJQDZZ)
	TextView tvJJQDZZ;
	@BindView(R.id.tvSFLS)
	TextView tvSFLS;
	@BindView(R.id.tvSFYHGF)
	TextView tvSFYHGF;
	@BindView(R.id.tvYHSFGF)
	TextView tvYHSFGF;
	@BindView(R.id.tvYQSFGF)
	TextView tvYQSFGF;

	String build_no;
	Dialog dialog;
	
	@BindView(R.id.gvXiu)
	GridView gvXiu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_data_input_dqrqaq_read);
		ButterKnife.bind(this);
		init();
	}

	public void init() {
		setTop();
		getIntentData();
		getData();
	}

	private void getIntentData() {
		build_no = getIntent().getStringExtra("build_no");
	}

	private void setTop() {
		tvTitle.setText("电气燃气安全");
		tvSearch.setVisibility(View.GONE);
	}

	@OnClick({ R.id.tvBack, R.id.tvSearch })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		default:
			break;
		}
	}

	private void getData() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());
		params.put("build_info_id", build_no);

		dialog = showDialog(dialog);
		VolleyRequest.StringRequestPost(this, WebApi.DATA_INPUT_DQRQAQ_READ,
				"", params, new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								String data = obj.getString("data");
								Log.e("dfdsf   ", result);
								DataInputDQRQAQBean temp = new Gson().fromJson(
										data, DataInputDQRQAQBean.class);
								if (temp == null) {
									temp = new DataInputDQRQAQBean();
									temp.setBuild_info_id(build_no);
									temp.setElectrical_equipment_capacity(0);
									temp.setElectrical_line_connect(0);
									temp.setEmergency_switchgear(0);
									temp.setEquipment_maintain(0);
									temp.setFire_service_load_no_standard(0);
									temp.setFuel_gas_add_no_standard(0);
									temp.setFuel_gas_equipment(0);
									temp.setGas_leakage_alarm_device(0);
									temp.setUse_ele_no_standard(0);
									temp.setUse_fire_no_standard(0);
									temp.setUse_gas_no_standard(0);
								}
								setData(temp);
							} else if (status.equals("404")
									&& info.contains("尚未登录")) {
								ToastUtil.getInstances().showMsg(
										"尚未登录或该用户在其他地方登录，请重新登录！");
								// 清除环信登录
								UserHelp.getInstance().clearUser(
										AppBase.Context, true);
								return;
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(
								R.string.err_http_response);
					}
				});
	}

	void setData(DataInputDQRQAQBean temp) {
		if (temp.getFire_service_load_no_standard() == 0) {
			tvYDFHBFHYQ.setText("否");
		} else {
			tvYDFHBFHYQ.setText("是");
		}
		if (temp.getElectrical_line_connect() == 0) {
			tvDQXLSFLJ.setText("否");
		} else {
			tvDQXLSFLJ.setText("是");
		}

		if (temp.getElectrical_equipment_capacity() == 0) {
			tvDQSBRLZT.setText("正常");
		} else if (temp.getElectrical_equipment_capacity() == 1) {
			tvDQSBRLZT.setText("超负荷");
		} else {
			tvDQSBRLZT.setText("安装不规范");
		}

		if (temp.getFuel_gas_add_no_standard() == 0) {
			tvBWJSBFH.setText("否");
		} else {
			tvBWJSBFH.setText("是");
		}

		if (temp.getFuel_gas_equipment() == 0) {
			tvAZSFFHBZ.setText("否");
		} else {
			tvAZSFFHBZ.setText("是");
		}

		if (temp.getGas_leakage_alarm_device() == 0) {
			tvXLBJZZ.setText("否");
		} else {
			tvXLBJZZ.setText("是");
		}

		if (temp.getEmergency_switchgear() == 0) {
			tvJJQDZZ.setText("否");
		} else {
			tvJJQDZZ.setText("是");
		}
		if (temp.getEquipment_maintain() == 0) {
			tvSFLS.setText("否");
		} else {
			tvSFLS.setText("是");
		}
		if (temp.getUse_fire_no_standard() == 0) {
			tvSFYHGF.setText("否");
		} else {
			tvSFYHGF.setText("是");
		}
		if (temp.getUse_ele_no_standard() == 0) {
			tvYHSFGF.setText("否");
		} else {
			tvYHSFGF.setText("是");
		}
		if (temp.getUse_gas_no_standard() == 0) {
			tvYQSFGF.setText("否");
		} else {
			tvYQSFGF.setText("是");
		}
		
		if (temp.getImage() == null || temp.getImage().size() <= 0) {
			gvXiu.setVisibility(View.GONE);
		} else {
			gvXiu.setVisibility(View.VISIBLE);
			//图片列表
			 List<String> picList = (List<String>) temp.getImage();
			 if (picList != null) {
				 final String [] urls = new String [picList.size()];
					for (int i = 0; i < picList.size(); i++) {
						urls[i] = picList.get(i);
					}
				 
					gvXiu.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						Intent intent = new Intent(view.getContext(),ImagePagerActivity.class);
						intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
						intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
						view.getContext().startActivity(intent);
					}
				});
				PicsAdapter picsAdapter = new PicsAdapter(gvXiu.getContext(),picList);
				gvXiu.setAdapter(picsAdapter);
			}
		}

	}
}
